/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package leapmotionfx;

import com.leapmotion.leap.CircleGesture;
import com.leapmotion.leap.Controller;
import com.leapmotion.leap.Finger;
import com.leapmotion.leap.Frame;
import com.leapmotion.leap.Gesture;
import com.leapmotion.leap.Gesture.State;
import com.leapmotion.leap.GestureList;
import com.leapmotion.leap.KeyTapGesture;
import com.leapmotion.leap.Listener;
import com.leapmotion.leap.ScreenTapGesture;
import com.leapmotion.leap.SwipeGesture;
import javafx.animation.PathTransition;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.shape.Rectangle;

public class SampleListener extends Listener {

    private PathTransition pathTransitionEllipse;
    private PathTransition pathTransitionCircle;
    private boolean circleRunning = true;
    private boolean ellipseRunning = true;
    private GraphicsContext gc;
    private Rectangle rect;
    
    public SampleListener(PathTransition pathTransitionEllipse, PathTransition pathTransitionCircle) {
        super();
        this.pathTransitionCircle = pathTransitionCircle;
        this.pathTransitionEllipse = pathTransitionEllipse;
    }
    
    public SampleListener(GraphicsContext gc) {
        super();
        this.gc = gc;
    }
    
    public SampleListener(Rectangle rect) {
        super();
        this.rect = rect;
    }
    
    @Override
    public void onInit(Controller cntrlr) {
        super.onInit(cntrlr);
    }

    @Override
    public void onConnect(Controller cntrlr) {
        super.onConnect(cntrlr);
        System.out.println("Connected");
        //cntrlr.enableGesture(Gesture.Type.TYPE_SWIPE);
        //cntrlr.enableGesture(Gesture.Type.TYPE_CIRCLE);
        cntrlr.enableGesture(Gesture.Type.TYPE_SCREEN_TAP);
        //cntrlr.enableGesture(Gesture.Type.TYPE_KEY_TAP);
    }

    @Override
    public void onDisconnect(Controller cntrlr) {
        super.onDisconnect(cntrlr);
    }

    @Override
    public void onExit(Controller cntrlr) {
        super.onExit(cntrlr);
    }

    @Override
    public void onFrame(Controller cntrlr) {
        super.onFrame(cntrlr);
        Frame frame = cntrlr.frame();
        
        Finger finger = frame.fingers().get(0);
        double x = 0;
        double y = 0;
            
        if (!frame.fingers().isEmpty()){
            x = frame.fingers().get(0).tipPosition().getX();
            y = frame.fingers().get(0).tipPosition().getY();
            
            if (x > 0.0 || y > 0.0){
                x = x+280 < 0 ? 0 : x+280;
                y = 650-y < 0 ? 0 : 650-y;
                /*System.out.print(x);
                System.out.print(" | ");
                System.out.println(y);*/
                
                try {
                    //gc.strokeOval(x, y, 5, 5);
                    rect.relocate(x, y);
                } catch (Exception e) {
                }
            }
        }
        
        GestureList gestures = frame.gestures();
        for (int i = 0; i < gestures.count(); i++) {
            Gesture gesture = gestures.get(i);

            switch (gesture.type()) {
                case TYPE_CIRCLE:
                    CircleGesture circle = new CircleGesture(gesture);
                    // Calculate clock direction using the angle between circle normal and pointable
                    String clockwiseness;
                    if (circle.pointable().direction().angleTo(circle.normal()) <= Math.PI / 4) {
                        // Clockwise if angle is less than 90 degrees
                        clockwiseness = "clockwise";
                    } else {
                        clockwiseness = "counterclockwise";
                    }

                    // Calculate angle swept since last frame
                    double sweptAngle = 0;
                    if (circle.state() != State.STATE_START) {
                        CircleGesture previousUpdate = new CircleGesture(cntrlr.frame(1).gesture(circle.id()));
                        sweptAngle = (circle.progress() - previousUpdate.progress()) * 2 * Math.PI;
                    }

                    System.out.println("Circle id: " + circle.id()
                            + ", " + circle.state()
                            + ", progress: " + circle.progress()
                            + ", radius: " + circle.radius()
                            + ", angle: " + Math.toDegrees(sweptAngle)
                            + ", " + clockwiseness);
                    break;
                case TYPE_SWIPE:
                    SwipeGesture swipe = new SwipeGesture(gesture);
                    System.out.println("Swipe id: " + swipe.id()
                            + ", " + swipe.state()
                            + ", position: " + swipe.position()
                            + ", direction: " + swipe.direction()
                            + ", speed: " + swipe.speed());
                    break;
                case TYPE_SCREEN_TAP:
                    ScreenTapGesture screenTap = new ScreenTapGesture(gesture);
                    
                    /*if (this.circleRunning){
                        this.pathTransitionCircle.pause();
                    } else {
                        this.pathTransitionCircle.play();
                    }
                    
                    
                    if (this.ellipseRunning) {
                        this.pathTransitionEllipse.pause();
                    } else {
                        this.pathTransitionEllipse.play();
                    }
                    
                    this.circleRunning = !circleRunning;
                    this.ellipseRunning = !ellipseRunning;*/
                    
                    System.out.print(x);
                    System.out.print(" | ");
                    System.out.println(y);
                
                    /*System.out.println("Screen Tap id: " + screenTap.id()
                            + ", " + screenTap.state()
                            + ", position: " + screenTap.position()
                            + ", direction: " + screenTap.direction());*/
                    break;
                case TYPE_KEY_TAP:
                    KeyTapGesture keyTap = new KeyTapGesture(gesture);
                    System.out.println("Key Tap id: " + keyTap.id()
                            + ", " + keyTap.state()
                            + ", position: " + keyTap.position()
                            + ", direction: " + keyTap.direction());
                    break;
                default:
                    System.out.println("Unknown gesture type.");
                    break;
            }
        }
    }
}
